%{
#include <stdio.h>
#include <string.h>
#include "operations.h"

fraction_t *frac;
%}

%union
{
  int number;
  char *string;
  fraction_t *fraction;
}

%token <string> MARK
%token <string> DIV
%token <string> MUL
%token <string> PLUS
%token <string> MINUS
%token <number> NUMBER
%token <number> END
%token <number> COMMENT
%token <number> OPEN
%token <number> CLOSE
%type <fraction> expr
%type <fraction> mulex
%type <fraction> term
%type <fraction> simple_number

%%
program: line program
         | line

line: expr END { 
                	printf("Result: ");
                	PrintFraction($1); 
                }
      | END
      | COMMENT END

expr: expr PLUS mulex { 
                     		$$ = Sum($1, $3, 1);
                      }
      | expr MINUS mulex { 
                     		$$ = Sum($1, $3, 0);
                       }
      | mulex { $$ = $1; }

mulex: mulex MUL term { $$ = Multiply($1, $3); }
       | mulex DIV term { $$ = Devide($1, $3); }
       | term { $$ = $1; }

term: OPEN expr CLOSE { $$ = $2; }
      | simple_number { $$ = $1; }

simple_number: NUMBER 
               {
               	frac = MakeFraction($1, 0, 1);
               	PrintFraction(frac);
               	$$ = frac;
               }
               | MARK NUMBER DIV NUMBER MARK
               {
               	frac = MakeFraction(0, $2, $4);
               	ReduceFraction(frac, 0);
               	PrintFraction(frac);
               	$$ = frac;
               }
               | NUMBER MARK NUMBER DIV NUMBER MARK
               {
               	frac = MakeFraction($1, $3, $5);
               	ReduceFraction(frac, 0);
               	PrintFraction(frac);
               	$$ = frac;
               }


%%

int yyerror(const char *str)
{
  fprintf(stderr,"error: %s\n",str);
}

int yywrap()
{
  return 1;
}



